<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    var xhr = new XMLHttpRequest();
    //设置超时时间，超过1秒后端没有返回数据，则取消本次ajax请求
    //超时后会触发 ontimeout事件
    xhr.timeout = 1000; 
    xhr.ontimeout = function(){
      alert('请求超时了')
    }
    xhr.open('get','/api/async');
    xhr.send();
    xhr.onload = function(){
      alert(xhr.responseText);
    };
  </script>
</body>
</html>